package com.gqk.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gqk.common.handler.GlobalExceptionHandler;
import com.gqk.common.result.Result;
import com.gqk.model.system.SysRole;
import com.gqk.service.SysRoleService;
import com.gqk.vo.system.SysRoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService service;
    @ApiOperation(value = "角色全部查询")
    @GetMapping("/findAll")
    public  Result<List<SysRole>> findAll() throws GlobalExceptionHandler {
        try {
            int a = 10/0;
        }catch (Exception e){
            throw new GlobalExceptionHandler();
        }
        List<SysRole> list = service.list();
        return Result.ok(list);
    }

    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result pageQueryRole(@PathVariable Long page,
                                @PathVariable Long limit,
                                SysRoleQueryVo sysRoleQueryVo){
        //创建Page对象
        Page<SysRole> pageParam = new Page<>(page,limit);
       /* LambdaQueryWrapper<SysRole> lambdaQueryWrapper = new LambdaQueryWrapper();
        String roleName = sysRoleQueryVo.getRoleName();
        if(!StringUtils.isEmpty(roleName)) {
            //封装 like模糊查询
            lambdaQueryWrapper.like(SysRole::getRoleName,roleName);
        }*/
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        String roleName = sysRoleQueryVo.getRoleName();
        if(!StringUtils.isEmpty(roleName)){
            queryWrapper.like("role_name",roleName);
        }
        IPage<SysRole> pageModel = service.page(pageParam, queryWrapper);
        return Result.ok(pageModel);
    }



}
